//
//  PostWindow.m
//  QuickPost
//
//  Created by Chen Xianan on 2/3/09.
//  Copyright 2009 sk. All rights reserved.
//

#import "PostWindow.h"

#define ARROW_WIDTH 30
#define ARROW_HEIGHT 10
#define ARROW_OFFLEFT 40
#define CORNER_RADIUS 20

@interface PostWindow ()
- (NSBezierPath *)_backgroundPath;
- (NSColor *)_backgroundImageColor;
@end

@implementation PostWindow

- (id)initWithView:(NSView *)view
{
	_viewRect = [view frame];
	_winRect = NSZeroRect;
	_winRect.size.height = _viewRect.size.height + ARROW_HEIGHT;
	_winRect.size.width = _viewRect.size.width;
    if (self = [super initWithContentRect:_winRect
                                styleMask:NSBorderlessWindowMask
                                  backing:NSBackingStoreBuffered
                                    defer:NO])

		{
			//[super setBackgroundColor:[NSColor clearColor]];
			[self setMovableByWindowBackground:NO];
			[self setExcludedFromWindowsMenu:YES];
			[self setAlphaValue:0.95];
			[self setOpaque:NO];
			[self setHasShadow:YES];
			[self useOptimizedDrawing:YES];
			[self setBackgroundColor:[self _backgroundImageColor]];
			[[self contentView] addSubview:view];
		}
    return self;
}

- (BOOL)canBecomeKeyWindow
{
	return YES;
}

- (NSBezierPath *)_backgroundPath
{
	NSBezierPath *bp = [NSBezierPath bezierPath];
	
	// [bp appendBezierPathWithRoundedRect:_viewRect
	// 					  xRadius:8
	// 					  yRadius:8];
	NSPoint startingPoint = NSMakePoint(_viewRect.origin.x + ARROW_OFFLEFT, _viewRect.size.height);
	[bp moveToPoint:startingPoint];
	[bp lineToPoint:NSMakePoint(_viewRect.origin.x + ARROW_OFFLEFT + ARROW_WIDTH/2, _winRect.size.height)];
	[bp lineToPoint:NSMakePoint(_viewRect.origin.x + ARROW_OFFLEFT + ARROW_WIDTH, _viewRect.size.height)];
	// [bp closePath];
	NSPoint cp;
	// top right corner
	[bp lineToPoint:NSMakePoint(_viewRect.size.width - CORNER_RADIUS, _viewRect.size.height)];
	cp = NSMakePoint(_viewRect.size.width, _viewRect.size.height);
	[bp curveToPoint:NSMakePoint(_viewRect.size.width, _viewRect.size.height - CORNER_RADIUS)
	   controlPoint1:cp
	   controlPoint2:cp];
	// bottom right corner
	[bp lineToPoint:NSMakePoint(_viewRect.size.width, _viewRect.origin.y + CORNER_RADIUS)];
	cp = NSMakePoint(_viewRect.size.width, _viewRect.origin.y);
	[bp curveToPoint:NSMakePoint(_viewRect.size.width - CORNER_RADIUS, _viewRect.origin.y)
	   controlPoint1:cp
	   controlPoint2:cp];
	// bottom left corner
	[bp lineToPoint:NSMakePoint(_viewRect.origin.x + CORNER_RADIUS, _viewRect.origin.y)];
	cp = NSMakePoint(_viewRect.origin.x, _viewRect.origin.y);
	[bp curveToPoint:NSMakePoint(_viewRect.origin.x, _viewRect.origin.y + CORNER_RADIUS)
	   controlPoint1:cp
	   controlPoint2:cp];
	// top left corner
	[bp lineToPoint:NSMakePoint(_viewRect.origin.x, _viewRect.size.height - CORNER_RADIUS)];
	cp = NSMakePoint(_viewRect.origin.x, _viewRect.size.height);
	[bp curveToPoint:NSMakePoint(_viewRect.origin.x + CORNER_RADIUS, _viewRect.size.height)
	   controlPoint1:cp
	   controlPoint2:cp];
	[bp lineToPoint:startingPoint];
	[bp closePath];
	
	return bp;
}

- (NSColor *)_backgroundImageColor
{
	NSImage *bg = [[NSImage alloc] initWithSize:_winRect.size];
    NSRect bgRect = NSZeroRect;
    bgRect.size = [bg size];
    
    [bg lockFocus];
	
    NSBezierPath *bgPath = [self _backgroundPath];
    [NSGraphicsContext saveGraphicsState];
    [bgPath addClip];
    
	[[NSColor blackColor] set];
	[bgPath fill];

	[bgPath setLineJoinStyle:NSRoundLineJoinStyle];
	[bgPath setLineWidth:3.0];
	[[NSColor whiteColor] set];
	[bgPath stroke];
	
    [NSGraphicsContext restoreGraphicsState];
    [bg unlockFocus];
    
    return [NSColor colorWithPatternImage:[bg autorelease]];
}

@end
